package com.example.goods.service.impl;

import com.example.goods.bean.ProductInfo;
import com.example.goods.common.ResultResponse;
import com.example.goods.repository.ProductInfoRepository;
import com.example.goods.service.ProductInfoService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *商品模块
 * @author ouyangzhixiang
 * @date 2018/7/4
 */

@Service
@AllArgsConstructor
public class ProductInfoServiceImpl implements ProductInfoService {
    private final ProductInfoRepository repository;
    /**
     * 添加商品信息
     * @param productInfo
     * @return
     */
    @Override
    public ResultResponse<ProductInfo> addProduct(ProductInfo productInfo) {
        ProductInfo addProduct = new ProductInfo();
        BeanUtils.copyProperties(productInfo,addProduct);
        return ResultResponse.createBySuccess(  repository.save(addProduct));
    }

    @Override
    /**
     * 查询商品列表按价格升序排列
     */
    public ResultResponse<List<ProductInfo>> ascending() {
        List<ProductInfo> productInfoList = repository.findAllByOrderByProductPriceAsc();
        if (productInfoList.isEmpty()){
            return ResultResponse.createByErrorMessage("商品信息不存在");
        }
        return ResultResponse.createBySuccess(productInfoList);
    }

    /**
     * 查询所有商品按商品价格降序排列
     * @return
     */
    @Override
    public ResultResponse<List<ProductInfo>> descending() {

        List<ProductInfo> productInfoList = repository.findAllByOrderByProductPriceDesc();
        if (productInfoList.isEmpty()){
            return ResultResponse.createByErrorMessage("商品信息不存在");
        }
        return ResultResponse.createBySuccess(productInfoList);
    }

}
